//
//  KZReviewTestPaperCell.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2019/1/7.
//  Copyright © 2019 zhukai. All rights reserved.
//

#import "KZReviewTestPaperCell.h"

// view
#import "KZProgressView.h"
#import "KZShadowButton.h"

@implementation KZReviewTestPaperCell
{
    UIImageView *_iconView;             // 背景（没做的：绿色，正在做的：蓝色，已作答的：橙色）
    
    UIImageView *_isNewView;            // 是否是最新的
    
    UILabel *_progressLabel;            // 进度
    KZProgressView *_progressView;      // 进度
    
    UILabel *_scoreLabel;               // 我的得分
    UILabel *_scoreDetailLabel;         // 战胜了多少人
    
    UILabel *_titleLabel;               // 试卷标题
    KZShadowButton *_funcButton;        // 功能按钮
    UILabel *_timeLabel;                // 可能是”发布时间（没做过）“，”最新作答时间（做过了没做完）“，”提交时间（做完了）“
    UILabel *_limitLabel;               // 期限时间，完成了就没有了
    UILabel *_submitCountLabel;         // 提交人数（完成就没有了）
    UILabel *_highestScoreLabel;        // 最高分
    UILabel *_averageScoreLabel;        // 平均分
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    

    UIView *line = [UIView new];
    line.backgroundColor = kLineColor;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.left.mas_equalTo(W(10));
        make.right.mas_equalTo(W(-10));
        make.height.equalTo(@0.5);
    }];
    
    _iconView = [UIImageView new];
    [self.contentView addSubview:_iconView];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(15));
        make.top.mas_equalTo(H(10));
        make.height.mas_equalTo(H(60));
        make.width.mas_equalTo(W(100));
    }];
    
    _isNewView = [UIImageView new];
    _isNewView.image = [UIImage imageNamed:@"kz-new标签"];
    [self.contentView addSubview:_isNewView];
    [_isNewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_iconView).offset(-1);
        make.top.equalTo(self->_iconView).offset(-1);
        make.width.height.mas_equalTo(H(30));
    }];

    _progressView = [KZProgressView new];
    [_iconView addSubview:_progressView];
    [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(12));
        make.right.mas_equalTo(W(-12));
        make.bottom.mas_equalTo(H(-6));
        make.height.mas_equalTo(H(6));
    }];
    
    NSArray *familyNames =[[NSArray alloc] initWithArray:[UIFont familyNames]];
    NSArray *fontNames;
    
//    NSLog(@"[familyNames count]===%lu",(unsigned long)[familyNames count]);
//    for(NSInteger familyCount=0;familyCount<[familyNames count]; familyCount++)
//    {
//        NSLog(@"Family name: %@", [familyNames objectAtIndex:familyCount]);
//        fontNames =[[NSArray alloc]initWithArray:[UIFont fontNamesForFamilyName:[familyNames objectAtIndex:familyCount]]];
//        for(NSInteger fontCount=0; fontCount<[fontNames count]; fontCount++)
//        {
//            NSLog(@"Font name: %@",[fontNames objectAtIndex:fontCount]);
//        }
//    }
 
    _progressLabel = [[UILabel alloc] init];
    _progressLabel.text = @"67%";
//    _progressLabel.font = [UIFont fontWithName:@"Oswald" size:24];
    _progressLabel.font = FONT(24);
    _progressLabel.textAlignment = NSTextAlignmentCenter;
    _progressLabel.textColor = UIColor.whiteColor;
    [_iconView addSubview:_progressLabel];
    [_progressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self->_iconView);
        
    }];
    
    _scoreDetailLabel = [[UILabel alloc] init];
    _scoreDetailLabel.adjustsFontSizeToFitWidth = true;
    _scoreDetailLabel.text = @"战胜56%的小伙伴";
    _scoreDetailLabel.font = FONT(12);
    _scoreDetailLabel.textAlignment = NSTextAlignmentCenter;
    _scoreDetailLabel.textColor = UIColor.whiteColor;
    [_iconView addSubview:_scoreDetailLabel];
    [_scoreDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self->_progressView);
    }];
    
    _scoreLabel = [[UILabel alloc] init];
    _scoreLabel.text = @"87分";
    _scoreLabel.font = FONT(24);
    _scoreLabel.textAlignment = NSTextAlignmentCenter;
    _scoreLabel.textColor = UIColor.whiteColor;
    [_iconView addSubview:_scoreLabel];
    [_scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self->_iconView);
    }];
    
    _funcButton = [[KZShadowButton alloc] init:^(UILabel *label) {
        label.text = @"作答";
        label.font = FONT(12);
    }];
    _funcButton.userInteractionEnabled = false;
    [self.contentView addSubview:_funcButton];
    [_funcButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(W(-15));
        make.width.mas_equalTo(W(40));
        make.height.mas_equalTo(H(20));
        make.top.equalTo(self->_iconView);
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 2;
    _titleLabel.text = @"初中英语系统复习试卷1";
    _titleLabel.font = FONT(15);
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.textColor = kBlueColor;
    [self.contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_iconView.mas_right).offset(W(10));
        make.top.equalTo(self->_iconView);
        make.right.lessThanOrEqualTo(self->_funcButton.mas_left).offset(W(-10));
    }];
    
    _submitCountLabel = [[UILabel alloc] init];
    _submitCountLabel.numberOfLines = 1;
    _submitCountLabel.text = @"提交人数：12/20 最高得分：100";
    _submitCountLabel.font = FONT(12);
    _submitCountLabel.textAlignment = NSTextAlignmentLeft;
    _submitCountLabel.textColor = UIColor.lightGrayColor;
    [self.contentView addSubview:_submitCountLabel];
    [_submitCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_titleLabel);
        make.top.equalTo(self->_titleLabel.mas_bottom).offset(H(5));
        make.right.mas_equalTo(-15);
    }];
    
    _limitLabel = [[UILabel alloc] init];
    _limitLabel.numberOfLines = 1;
    _limitLabel.text = @"期限：2019/01/01 12:12 - 2019/02/01 12:12";
    _limitLabel.font = FONT(12);
    _limitLabel.textAlignment = NSTextAlignmentLeft;
    _limitLabel.textColor = UIColor.lightGrayColor;
    [self.contentView addSubview:_limitLabel];
    [_limitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_submitCountLabel);
        make.top.equalTo(self->_submitCountLabel.mas_bottom).offset(H(2));
        make.right.equalTo(self->_submitCountLabel);
    }];
    
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.adjustsFontSizeToFitWidth = true;
    _timeLabel.numberOfLines = 1;
    _timeLabel.text = @"发布时间：2019/01/01 18:18";
    _timeLabel.font = FONT(12);
    _timeLabel.textAlignment = NSTextAlignmentLeft;
    _timeLabel.textColor = UIColor.lightGrayColor;
    [self.contentView addSubview:_timeLabel];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_limitLabel);
        make.top.equalTo(self->_limitLabel.mas_bottom).offset(H(2));
        make.right.equalTo(self->_limitLabel);
        
        make.bottom.mas_equalTo(-H(10));
    }];
}


#pragma mark - setter
- (void)setTestStatus:(int)testStatus {
    
    _testStatus = testStatus;
    switch (_testStatus) {
        case 0: // 未作答
        {
            _funcButton.label.text = @"作答";
            
            _iconView.image = [UIImage imageNamed:@"kz-未作答试卷背景"];
            _isNewView.hidden = false;
            _progressLabel.hidden = true;
            _progressView.hidden = true;
            _scoreLabel.hidden = true;
            _scoreDetailLabel.hidden = true;
        }
            break;
        case 1: // 已作答，未完成
        {
            _funcButton.label.text = @"作答";
            
            _iconView.image = [UIImage imageNamed:@"kz-作答中试卷背景"];
            _isNewView.hidden = true;
            _progressLabel.hidden = false;
            _progressView.hidden = false;
            _scoreLabel.hidden = true;
            _scoreDetailLabel.hidden = true;
            
            CGFloat progress = 0.67;
            _progressView.progress = progress;
            NSString *progressText = [NSString stringWithFormat:@"%.0lf", progress * 100];
            NSString *text = [NSString stringWithFormat:@"%@%%", progressText];
            _progressLabel.attributedText = [text attributeStringWithAttributes:@{NSFontAttributeName: FONT(12)} scope:@"%"];
        }
            break;
        case 2: // 已完成
        {
            _funcButton.label.text = @"查看";
            
            _iconView.image = [UIImage imageNamed:@"kz-已完成试卷背景"];
            _isNewView.hidden = true;
            _progressLabel.hidden = true;
            _progressView.hidden = true;
            _scoreLabel.hidden = false;
            _scoreDetailLabel.hidden = false;
            
            NSString *scoreText = [NSString stringWithFormat:@"%i", 87];
            NSString *text = [NSString stringWithFormat:@"%@分", scoreText];
            _scoreLabel.attributedText = [text attributeStringWithAttributes:@{NSFontAttributeName: FONT(12)} scope:@"分"];

        }
            break;
            
        default:
            break;
    }
}

@end
